package com.ruoyi.faceApi.click.impl;

import com.ruoyi.faceApi.click.FaceApiClick;
import com.ruoyi.faceApi.click.FaceCallBack;
import com.ruoyi.faceApi.click.IFaceCallBack;
import com.ruoyi.faceApi.thrift.ThriftClient;
import com.ylsoft.android.iacterminal.net.thrift.server.transaction.AIFTThriftService;
import com.ylsoft.android.iacterminal.net.thrift.server.transaction.TWhiteListPerson;
import org.apache.thrift.TException;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: wwy
 * @Date: 2021/6/23 10:48
 */
public class FaceApiDefaultClick implements FaceApiClick {
    private static final int TIMEOUT = 10000;
    public static ConcurrentHashMap<String,AIFTThriftService.Client> connectMap = new ConcurrentHashMap<>();
    /**
     * 连接设备
     * @param ip
     * @param port
     */
    @Override
    public void connect(String ip, int port) {
        AIFTThriftService.Client thriftclient = ThriftClient.GetInstance().GetThriftClient(ip,port,TIMEOUT);
        if(thriftclient == null){
            //连接失败
            System.out.println("设备连接失败，ip:"+ip);
        }else{
            System.out.println("设备连接成功，ip:"+ip);
            connectMap.put(ip,thriftclient);
        }

    }

    /**
     * 添加白名单
     * @param tPerson 人员信息
     * @param ip 设备ip
     * @return
     */
    @Override
    public int addWhiteList(TWhiteListPerson tPerson,String ip) {
        AIFTThriftService.Client client = connectMap.get(ip);
        if(client == null){
            return -1;
        }
        int i = -1;
        try {
            i = client.FTTS_AddWhiteListPerson(tPerson);
        } catch (TException e) {
            e.printStackTrace();
        }
        return i;
    }

    /**
     * 删除白名单
     * @param ip
     * @param personId
     * @return
     */
    @Override
    public int deleteWhiteListPerson(String ip, String personId) {
        AIFTThriftService.Client client = connectMap.get(ip);
        if(client == null){
            return -1;
        }
        int i = -1;
        try {
            i = client.FTTS_DeleteWhiteListPersonByPersonId(personId);
        } catch (TException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 设置回调函数
     * @param faceCallBack
     */
    @Override
    public void setFaceCallBack(IFaceCallBack faceCallBack) {
        FaceCallBack.faceCallBack = faceCallBack;
    }
}
